package com.abner.activiti.query;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.io.IOUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @description:
 * @author: guoc
 * @createDate: 2020/9/11 15:34
 */
public class GetActivitiFile {

    public void getProcessResources() throws IOException {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //查询器
        ProcessDefinitionQuery getProcessResources = repositoryService.createProcessDefinitionQuery();

        //过滤查询
        ProcessDefinitionQuery game = getProcessResources.processDefinitionKey("game");
        ProcessDefinition processDefinition = game.singleResult();
        //部署ID
        String deploymentId = processDefinition.getDeploymentId();
        //输入流:第一个参数，部署ID，第二个参数，资源名称
        InputStream pngIn = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName());
        InputStream bpmnIn = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());
        //输出流
        FileOutputStream pngOut = new FileOutputStream(
                "d:\\" + processDefinition.getDiagramResourceName());
        FileOutputStream bpmnOut = new FileOutputStream("d:\\" + processDefinition.getResourceName());

        //流转换
        IOUtils.copy(pngIn, pngOut);
        IOUtils.copy(bpmnIn, bpmnOut);

        //关流
        pngOut.close();
        bpmnOut.close();
        pngIn.close();
        bpmnIn.close();
    }


    public static void main(String[] args) throws IOException {
        GetActivitiFile getActivitiFile =new GetActivitiFile();
        getActivitiFile.getProcessResources();
    }


}
